package com.bistu.openlab.uofspreadsheet.model;

import java.util.ArrayList;
import java.util.List;

/*
 *@author xiaoguang.gao@oracle.com
 *@version 2013-5-9 
 */
public class ExcelSheetContent {
	private MaxRowCol maxRowCol = null;
	private DefaultHeightWidth defaultHW = null;
	private List<SheetRow> rowList = null;
	
	public ExcelSheetContent(){
		this.maxRowCol = new MaxRowCol();
		this.defaultHW = new DefaultHeightWidth();
	}
	/**
	 * get a row in sheet
	 * @param rowid
	 * @return
	 */
	public SheetRow getRow(int rowid){
		if(this.rowList!=null){
			for(SheetRow row : this.rowList){
				if(row.getRowNum()==rowid)
					return row;
			}
		}
		return null;
	}
	/**
	 * set value in a cell located by rowid and cellid.
	 * @param rowid
	 * @param cellid
	 * @param value
	 */
	public void setValue(int rowid,int cellid,Object value){
		SheetRow row = this.getRow(rowid);
		if(row==null){
			row = new SheetRow();			
			row.setRowNum(rowid);
			this.addSheetRow(row);
		}		
		SheetCell cell = row.getCell(cellid);
		if(cell==null){
			cell = new SheetCell();
			cell.setCellNum(cellid);	
			row.addSheetCell(cell);
		}
		
		if(value!=null){
			cell.setText(value.toString());
			if(this.maxRowCol.getMaxRow()<rowid)
				this.maxRowCol.setMaxRow(rowid);
			if(this.maxRowCol.getMaxCol()<cellid)
				this.maxRowCol.setMaxCol(cellid);
		}
	}
	/**
	 * 
	 * @param rowid
	 * @param cellid
	 * @return
	 */
	public Object getValue(int rowid,int cellid){
		SheetRow row = this.getRow(rowid);
		if(row!=null){
			SheetCell cell = row.getCell(cellid);
			if(cell!=null){
				return cell.getText();
			}
		}
		return null;
	}
	
	public void addSheetRow(SheetRow row){
		if(this.rowList==null)
			this.rowList = new ArrayList<SheetRow>();
		this.rowList.add(row);
	}
	public void removeSheetRow(SheetRow row){
		if(this.rowList!=null){
			this.rowList.remove(row);
			if(this.rowList.size()<=0)
				this.rowList = null;
		}
	}
	public MaxRowCol getMaxRowCol() {
		return maxRowCol;
	}
	public void setMaxRowCol(MaxRowCol maxRowCol) {
		this.maxRowCol = maxRowCol;
	}
	public DefaultHeightWidth getDefaultHW() {
		return defaultHW;
	}
	public void setDefaultHW(DefaultHeightWidth defaultHW) {
		this.defaultHW = defaultHW;
	}
	public List<SheetRow> getRowList() {
		return rowList;
	}
	public void setRowList(List<SheetRow> rowList) {
		this.rowList = rowList;
	}
}
